from utils.jsl_data_map import default_columns
from strategy.StrategyTemplate import StrategyTemplate
from strategy import strategy_filter
from strategy.base_strategy.base_strategy_common import *
from utils.db import DbHandler

class TurnoverRank(StrategyTemplate):
    """
    换手率策略:
        按换手率排序

    """
    def __init__(self):
        super(TurnoverRank, self).__init__()
        self.strategy_name = "按换手率排序策略"
        self.strategy_descrption = "按照换手率从大到小排序"

    def run(self):
        # 基础过滤，过滤掉新债、强赎、EB
        self.data = strategy_filter.filter_base(self.data)
        # 筛选标准
        self.data = self.data.sort_values(by='换手率', ascending=False)
        # 获得默认列
        self.data = self.data[default_columns]
        temp_d = self.data.pop('换手率')
        # self.data_tool.insert(0, '换手率', temp_d)
        self.data['换手率'] = temp_d
        # 清掉原来的索引
        self.data.reset_index(inplace=True, drop=True)

    def api(self, size=10):
        db = DbHandler()
        db.create_session()
        super(self.__class__, self).api(db=db)
        # sort
        self.data = self.data.sort_values(by='turnover_rt', ascending=False)
        # 获得默认列
        self.data = self.data[turnover_column][:size]

        # 清掉原来的索引
        self.data.reset_index(inplace=True, drop=True)
        self.db.close_session()
        return self.data
if __name__ == '__main__':
    ins = TurnoverRank()
    ins.topN = 20
    ins.test()

